'''
基础层：封装元素定位和操作
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import logging

logging.basicConfig(filename='log.log', level=logging.INFO, format='%(asctime)s %(levelname)s %(name)s %(message)s')


class BasicPage():
    def __init__(self, driver):
        '''初始化谷歌浏览器'''
        self.driver = driver

    def get_url(self, url):
        '''打开url页面'''
        self.driver.get(url)

    def find_ele(self, loc):
        '''实现元素定位'''
        ele = self.driver.find_element(*loc)
        logging.info(f'定位{loc}成功')
        return ele

    def ele_click(self, loc):
        """点击登录按钮"""
        ele = self.find_ele(loc)
        ele.click()
        logging.info(f'点击{loc}成功')

    def clear_ele(self, loc):
        """输入框清空数据"""
        ele = self.find_ele(loc)
        ele.clear()
        logging.info(f'在{loc}清除文本成功')

    def input_ele(self, loc, value):
        """输入框输入数据"""
        ele = self.find_ele(loc)
        ele.send_keys(value)
        logging.info(f'在{loc}输入{value}成功')


if __name__ == '__main__':
    url = r'http://192.168.226.131/upload/'
    ls_username_loc = (By.ID, 'ls_username')
    ls_password_loc = (By.ID, 'ls_password')
    loc = (By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button/em')
    value1='admin'
    value2='admin'

    # 指定谷歌浏览器
    chrome_location = r'D:\python\chrome114\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location

    driver = webdriver.Chrome(options=options)

    basic_page = BasicPage(driver)
    basic_page.get_url(url)
    # basic_page.find_ele(loc)
    basic_page.clear_ele(ls_username_loc)
    basic_page.clear_ele(ls_password_loc)
    basic_page.input_ele(ls_username_loc,value1)
    basic_page.input_ele(ls_password_loc,value2)
    basic_page.ele_click(loc)
    sleep(3)
    driver.quit()
